package com.hcbd.tdwt.ui.xingyu.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;

import java.text.MessageFormat;
import java.util.List;

public class MapFriendsAdapter extends RecyclerView.Adapter<MapFriendsAdapter.MyViewHolder> {

    protected final Context mContext;
    private List<BoxContact> boxContacts;
    private final LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    public int type=0;
    public MapFriendsAdapter(Context context, List<BoxContact> list) {
        mContext = context;
        mInflater = ((Activity) context).getLayoutInflater();
        boxContacts = list;
    }
    public MapFriendsAdapter(Context context, List<BoxContact> list,int type) {
        mContext = context;
        mInflater = ((Activity) context).getLayoutInflater();
        boxContacts = list;
        this.type=type;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.map_friends_item, viewGroup, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        BoxContact boxContact = boxContacts.get(i);
        setBgType(myViewHolder);
        if(boxContact.getLatitude()!=null&&boxContact.getLongitude()!=null){
            myViewHolder.positionTV.setText(MessageFormat.format("经度:{0}纬度:{1}", boxContact.getLongitude(), boxContact.getLatitude()));
        }
        if(boxContact.getIsPayAttention()==0){
            myViewHolder.payAttentionImg.setBackground(mContext.getResources().getDrawable(R.drawable.no_focus));
        }else{
            myViewHolder.payAttentionImg.setBackground(mContext.getResources().getDrawable(R.drawable.focus_on));
        }
        myViewHolder.time.setText(DateUtils.simplifyTime(boxContact.getRecentCommunicationTime()));
        myViewHolder.friendsName.setText(boxContact.getName());
        myViewHolder.avaImg.setOnClickListener(view -> onItemClickListener.onClick(boxContact));
        myViewHolder.payAttentionImg.setOnClickListener(view -> onItemClickListener.onPayAttention(boxContact));
    }
    public void setBgType(MyViewHolder myViewHolder){
        if(type==1) {
            myViewHolder.mapFriendsLL.setBackground(mContext.getDrawable(R.color.white));
            myViewHolder.positionTV.setTextColor(mContext.getColor(R.color.black));
            myViewHolder.friendsName.setTextColor(mContext.getColor(R.color.black));
            myViewHolder.time.setTextColor(mContext.getColor(R.color.black));
        }
    }
    @Override
    public int getItemCount() {
        return boxContacts.size();
    }

    public void notifyDataSetChanged(List<BoxContact> list) {
        boxContacts = list;
        notifyDataSetChanged();
    }



    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView friendsName,time,positionTV;
        ImageView payAttentionImg,dangerImg,avaImg;
        LinearLayout mapFriendsLL;
        MyViewHolder(View itemView) {
            super(itemView);
            friendsName=itemView.findViewById(R.id.friendsName);
            time=itemView.findViewById(R.id.time);
            payAttentionImg=itemView.findViewById(R.id.payAttentionImg);
            dangerImg=itemView.findViewById(R.id.dangerImg);
            positionTV=itemView.findViewById(R.id.positionTV);
            mapFriendsLL=itemView.findViewById(R.id.mapFriendsLL);
            avaImg=itemView.findViewById(R.id.avatar_img);
        }
    }

    public interface OnItemClickListener {
        void onPayAttention(BoxContact boxContact);
        void onClick(BoxContact boxContact);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
